package com.example.attempt.controller;

import com.example.attempt.entity.*;
import com.example.attempt.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class AccountController {

    @Autowired
    private StudentServiceImpl studentImpl;
    @Autowired
    private TeacherServiceImpl teacherImpl;
    @Autowired
    private AssistantServiceImpl assistantImpl;
    @Autowired
    private CourseServiceImpl courseImpl;
    @Autowired
    private SelectCourseServiceImpl selectImpl;
    @Autowired
    private AdministratorServiceImp administratorServiceImp;
    @Autowired
    private SubmitServiceImpl submitService;
    @Autowired
    private PostServiceImpl postService;
    @Autowired
    private CommentServiceImpl commentService;
    @Autowired
    private AssessmentServiceImpl assessmentService;
    @Autowired
    private HomeworkServiceImpl homeworkService;
    @GetMapping("/getTeachers")
    public List<String> getTeachers(){
        List<String> teachers=new ArrayList<>();
        List<Teacher> ts=teacherImpl.queryAll();
        for (Teacher t : ts) {
            teachers.add(t.getTid());
        }
        return teachers;
    }
    @PostMapping("/teacherLogin")
    public Teacher teacherLogin(@RequestParam(value="tid") String tid, @RequestParam(value = "tpwd") String tpwd){
        System.out.println(tid + " " + tpwd);
        Teacher teacher = teacherImpl.queryByIdAndP(tid, tpwd);
        if(teacher == null){
            teacher = new Teacher();
            teacher.setTid("-1");
            teacher.setTpwd("-1");
            teacher.setTname("Wrong password or account number");
        }
        return teacher;
    }
    @PostMapping("/administratorLogin")
    public Administrator administratorLogin(@RequestParam(value = "aid") String aid,@RequestParam(value = "pwd") String pwd){

        Administrator administrator= administratorServiceImp.queryById(aid);
        System.out.println(administrator);
        if(administrator == null){
            administrator = new Administrator();
            administrator.setAdid("-1");
            administrator.setAdpwd("-1");
            administrator.setAdname("Wrong account number");
        }
        else if(!administrator.getAdpwd().equals(pwd)){
            administrator = new Administrator();
            administrator.setAdid("-2");
            administrator.setAdpwd("-2");
            administrator.setAdname("Wrong password");
        }
        return administrator;
    }
    //学生登录
    @PostMapping(path="/studentLogin")
    public Student getStudent(@RequestParam(value = "sid") String sid, @RequestParam(value = "spwd") String spwd){
        System.out.println("登录了捏");
        Student student = studentImpl.queryByIdAndP(sid, spwd);
        if(student == null){
            student = new Student();
            student.setSid("-1");
            student.setSname("Wrong password or account number");
            student.setSpwd("-1");
        }
        System.out.println(student);
        return student;
    }

    //助教登录
    @PostMapping("/assistantLogin")
    public Assistant assistantLogin(@RequestParam(value="aid") String aid, @RequestParam(value = "apwd") String apwd){
        System.out.println(aid + " " + apwd);
        Assistant assistant = assistantImpl.queryByIdAndP(aid, apwd);
        if(assistant == null){
            assistant = new Assistant();
            assistant.setAid("-1");
            assistant.setApwd("-1");
            assistant.setAname("Wrong password or account number");
        }
        return assistant;
    }

    //批量增加学生账号
    @PostMapping(path = "/addStudents")
    public int addStudents(@RequestBody List<Student> students){
//        Student student =new Student();
        for (Student student : students) {
            //            student.setSid(student.getSid());
            studentImpl.addStudent(student);
        }
        return 1;
    }

    //添加学生账号
    @PostMapping(path = "/addStudent")
    public int addStudent(@RequestBody Student student){
        if(studentImpl.queryById(student.getSid())!=null){
            return -1;
        }
        else{
//            student.setSid(student.getSid());
            studentImpl.addStudent(student);
            return 1;
        }
    }

    //查看学生账号
    @RequestMapping(path = "/getStudent")
    public Student getStudent(@RequestParam(value = "sid") String sid){
        if(studentImpl.queryById(sid)==null){
            Student student = new Student();
            student.setSid("-1");
            return student;
        }
        return studentImpl.queryById(sid);
    }

    //删除学生账号
    @PostMapping(path = "/deleteStudent")
    public int deleteStudent(@RequestParam(value = "sid") String sid){
//        System.out.println("阿承不给进来---删除阿承了捏");
        System.out.println(sid);
        if (studentImpl.queryById(sid)==null){
            return -5;
        }
        else if (!submitService.queryBySid(sid).isEmpty()){
            return -4;
        }
        else if (!selectImpl.queryBySid(sid).isEmpty()){
            return -3;
        }
        else if (!postService.queryBySid(sid).isEmpty()){
            return -2;
        }
        else if (!commentService.queryBySid(sid).isEmpty()){
            return -1;
        }
        else if (!assessmentService.queryBySid(sid).isEmpty()){
            return 0;
        }
        else{
            studentImpl.deleteStudent(sid);
            return 1;
        }
    }

    //显示所有学生账号
    @RequestMapping(path = "/list")
    public List<Student> list(){
        return studentImpl.queryAll();
    }

    //删除老师账号
    @PostMapping(path = "/deleteTeacher")
    public int deleteTeacher(@RequestParam(value = "tid") String tid){
        System.out.println("要删除的老师是"+tid);
        if (teacherImpl.queryById(tid)==null){
            return -6;
        }
        else if(!courseImpl.queryByTeacher(tid).isEmpty()){
            return -5;
        }
        else if(assistantImpl.queryByTid(tid)==null){
            return -4;
        }
        else if(!homeworkService.queryByTid(tid).isEmpty()){
            return -3;
        }
        else if (!postService.queryByTid(tid).isEmpty()){
            return -2;
        }
        else if (!commentService.queryByTid(tid).isEmpty()){
            return -1;
        }
        else if (!assessmentService.queryByTid(tid).isEmpty()){
            return 0;
        }
        else{
            teacherImpl.deleteTeacher(tid);
            List<Course> courses = courseImpl.queryByTeacher(tid);
            for (Course c : courses) {
                courseImpl.changeTid(c.getCid(), " ");
            }
            return 1;
        }
    }

    @PostMapping(path = "/addTeachers")
    //批量增加老师账号
    public void addTeachers(@RequestBody List<Teacher> teachers){
//        Teacher teacher = new Teacher();
        for (Teacher teacher : teachers) {
            teacher.setTid(teacher.getTid());
            teacherImpl.addTeacher(teacher);
        }
    }

    @PostMapping(path = "/addTeacher")
    //增加老师账号
    public int addTeachers(@RequestBody Teacher teacher){
        if (teacherImpl.queryById(teacher.getTid())!=null){
            return -1;
        }
        else{
            teacherImpl.addTeacher(teacher);
            return 1;
        }
    }
    @PostMapping(path = "/getTeacher")
    public Teacher getTeacher(@RequestParam(value = "tid") String tid){
        Teacher t=teacherImpl.queryById(tid);
        if (t==null){
            t=new Teacher();
            t.setTid("-1");
        }
        return t;
    }

    @PostMapping(path = "/isTeaching")
    public String isTeaching(@RequestParam(value = "tid") String tid){
        System.out.println("aaaa"+tid);
        if(!courseImpl.queryByTeacher(tid).isEmpty()){
            System.out.println(courseImpl.queryByTeacher(tid).toString());
            return "-1";
        }

        else {
            System.out.println(tid);
            return tid;
        }
    }

    //显示所有老师账号
    @RequestMapping(path = "/list2")
    public List<Teacher> listTeacher(){
        return teacherImpl.queryAll();
    }

    //删除助教账号
    @PostMapping(path = "/deleteAssistant")
    public int deleteAssistant(@RequestParam(value = "aid") String aid){
        System.out.println("要删除的助教是"+aid);
        if (assistantImpl.queryById(aid)==null){
            return -1;
        }
        else{
            assistantImpl.deleteAssistant(aid);
            return 0;
        }
    }

    @PostMapping(path = "/addAssistants")
    //批量增加助教账号
    public void addAssistants(@RequestBody List<Assistant> assistants){
//        Assistant assistant = new Assistant();
        for (Assistant assistant : assistants) {
            assistant.setAid(assistant.getAid());
            assistantImpl.addAssistant(assistant);
        }
    }

    @PostMapping(path = "/addAssistant")
    //增加助教账号
    public int addAssistants(@RequestBody Assistant assistant){
        if (assistantImpl.queryById(assistant.getAid())!=null){
            return 1;//已存在该助教
        }
        else if(teacherImpl.queryById(assistant.getTid())==null){
            return 2;//不存在该老师
        }
        else if(assistantImpl.queryByTid(assistant.getTid())!=null){
            return 3;//该老师已有助教
        }
        else{
            assistantImpl.addAssistant(assistant);
            return 0;
        }
    }
    @PostMapping(path = "/getAssistant")
    //获取助教账号
    public Assistant getAssistant(@RequestParam(value = "aid") String aid){
        Assistant a = assistantImpl.queryById(aid);
        if (a == null){
            a = new Assistant();
            a.setAid("-1");
        }
        return a;
    }

}
